package com.lu.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lu.entity.Phone;
import com.lu.entity.User;
import com.lu.phone.utils.MobileCodeWS;
import com.lu.phone.utils.MobileCodeWSSoap;
import com.lu.service.PhoneService;
import com.lu.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

public class DoRegisterAction extends ActionSupport{
	
	UserService userService = new UserService();
	PhoneService phoneService = new PhoneService();
	
	private String username;
	private String password;
	private String phoneNO;

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	public String execute(){
		try {
			Map<String,Object> map = new HashMap<>();
			map.put("username", username);
			User checkUser = userService.getUser(map);
			if(checkUser.getId()!=null){
				return "error";
			}
			
			User user = new User();
			user.setUsername(username);
			user.setPassword(password);
			user.setPhone(phoneNO);
			user.setStatus(1);
			Integer userId = userService.save(user);
			if(phoneNO!=null||!phoneNO.equals("")){
				if(userId!=null){
					MobileCodeWS mobileCodeWS = new MobileCodeWS();
					MobileCodeWSSoap mobileCodeWSSoap= mobileCodeWS.getMobileCodeWSSoap();
					String str = mobileCodeWSSoap.getMobileCodeInfo(phoneNO, null);
					System.out.println(str);
					System.out.println();
					int checkPhoneNO = str.indexOf("：");
					str = str.substring(str.indexOf("：")+1, str.length());
					String[] ss = str.split(" ");
					
					if(checkPhoneNO!=-1){
						Phone phone = new Phone();
						for(int i=0;i<ss.length;i++){
							phone.setProvince(ss[0]);
							phone.setCity(ss[1]);
							phone.setCardType(ss[2]);
						}
						phone.setUserId(Long.valueOf(userId));
						phoneService.save(phone);
					}
				}
			}
		} catch (Exception e) {
			return "success";
		}
		return "success";
	}


	public String getUsername() {
		return username;
	}


	public void setUsername(String username) {
		this.username = username;
	}


	public String getPassword() {
		return password;
	}


	public void setPassword(String password) {
		this.password = password;
	}


	public String getPhoneNO() {
		return phoneNO;
	}


	public void setPhoneNO(String phoneNO) {
		this.phoneNO = phoneNO;
	}

}
